package com.bootdo.wechat.controller;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.service.FileService;
import com.bootdo.system.domain.MenuDO;
import com.bootdo.system.service.MenuService;
import com.bootdo.wechat.domain.MpConfigDO;
import com.bootdo.wechat.service.MpConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author songj
 * @version ${VERSION}
 * @Title: bootdo-wechat
 * @Package com.bootdo.wechat.controller
 * @Description: ${TOTO} (用一句话描述该文件做什么)
 * @date 2018/8/2  14:28
 */
@Controller
@RequestMapping("/wechat/mpManage")
public class MpManageController extends MpBaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MenuService menuService;
    @Autowired
    FileService fileService;
    @Autowired
    MpConfigService mpConfigService;

    /**
     *
     * @param mpId
     * @param model
     * @return
     */
     @GetMapping("/{mpId}")
     String toManage(@PathVariable("mpId") String mpId, Model model){
         List<Tree<MenuDO>> menus = menuService.listMenuTree4Mp(getUserId(),mpId);
         model.addAttribute("menus",menus);
         model.addAttribute("name",getUser().getName());
         MpConfigDO mpConfigDO = mpConfigService.selectById(mpId);
         model.addAttribute("mpName",mpConfigDO.getMpName());
         if(mpConfigDO!=null&&mpConfigDO.getMpPicture()!=null){
             if(fileService.isExist(mpConfigDO.getMpPicture())){
                 model.addAttribute("picUrl",mpConfigDO.getMpPicture());
             }else {
                 model.addAttribute("picUrl","/img/photo_s.jpg");
             }
         }else {
             model.addAttribute("picUrl","/img/photo_s.jpg");
         }
         model.addAttribute("username", getUser().getUsername());
         return "index_v2";
     }
}
